//
//  BRMInfoDayView.m
//  BrezzeSaaS
//
//  Created by macos on 16.3.22.
//  Copyright © 2022 Brezze GWG. All rights reserved.
//

#import "BRMInfoDayView.h"
#import "BRMSwitchUIView.h"
#import "BRMDayTimeView.h"

@interface BRMInfoDayView ()
{
    UIView *_headerView;
    UIView *_contentView;
    QMUILabel *_titleL;
    QMUILabel *_hoursL;
    UIView *_timeBGView;
    BRMSwitchUIView *_daySwitchBtn;
    BRMSwitchUIView *_allTimeSwitchBtn;
    UIView *_lineView;
}

@property MASConstraint * headViewBottomContact0;
@property MASConstraint * headViewBottomContact1;

@end

@implementation BRMInfoDayView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)setIsOn:(BOOL)isOn {
    _isOn = isOn;
    _contentView.hidden = !isOn;
    _daySwitchBtn.isOn = isOn;
    if (isOn) {
        [self.headViewBottomContact1 install];
        [self.headViewBottomContact0 uninstall];
    }else {
        [self.headViewBottomContact0 install];
        [self.headViewBottomContact1 uninstall];
    }
}

- (void)setLineHidde:(BOOL)lineHidde {
    _lineHidde = lineHidde;
    _lineView.hidden = lineHidde;
}

- (void)setIsHoursOn:(BOOL)isHoursOn {
    _isHoursOn = isHoursOn;
    _allTimeSwitchBtn.isOn = isHoursOn;
    if (isHoursOn) {
        _hoursL.alpha = 1;
    }else {
        _hoursL.alpha = 0.4;
    }
    
    [self enbleTimeView:isHoursOn];
}

- (void)setTitlsStr:(NSString *)titlsStr {
    _titlsStr = titlsStr;
    _titleL.text = titlsStr;
}

- (void)enbleTimeView:(BOOL)hidden {
    for (int i = 0; i < 4; i++) {
        BRMDayTimeView *dayView = (BRMDayTimeView *)[_contentView viewWithTag:100 + i];
        dayView.isEnble = !hidden;
    }
}

- (void)setCount:(NSInteger)count {
    _count = count;
    
    
    CGFloat dayView_h = 46.;
    if (count >= 2) {
        dayView_h = 92.;
    }
    [_timeBGView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(dayView_h);
    }];
    
}

- (void)toSetTime:(UITapGestureRecognizer *)tap {
    NSLog(@"%s - %ld",__func__,tap.view.tag);
    
    BRMDayTimeView *dayView = (BRMDayTimeView *)tap.view;
    if (!dayView.isEnble) return;
    NSInteger index = dayView.tag - 100;
    if (dayView.isAdd) {
        self.count = index + 1;
        [self add:index];
        return;
    }
    if (self.timeAddBlock) {
        self.timeAddBlock(index);
    }
}
- (void)add:(NSInteger)index {
    
    for (int i = 0; i < 4; i++) {
        BRMDayTimeView *dayView = (BRMDayTimeView *)[_contentView viewWithTag:100 + i];
        dayView.isAdd = (i > index);
        dayView.isCanDele = NO;
        dayView.hidden = (i > index + 1);
        if (i == index && index != 0) {
            dayView.isCanDele = YES;
        }
//        if (i > index) {
//            dayView.isAdd = YES;
//        }
//        if (i > index + 1) {
//            dayView.isAdd = YES;
//            dayView.hidden = YES;
//        }

//        if (i == index - 1) {
//            dayView.isAdd = YES;
//
//        }
    }
}

- (void)deleteDayTime:(BRMDayTimeView *)dayView {
    
    NSInteger index = dayView.tag - 100;
    [self lastAdd:index];
    
    if (self.deleteTimeBlock) {
        self.deleteTimeBlock(index);
    }
}

- (void)lastAdd:(NSInteger)index {
    self.count = index;
    for (int i = 0; i < 4; i++) {
        BRMDayTimeView *dayView = (BRMDayTimeView *)[_contentView viewWithTag:100 + i];
        dayView.isAdd = (i >= index) ;
        dayView.isCanDele = NO;
        dayView.hidden = (i > index );

        if (i == index - 1 && index - 1 != 0) {
            dayView.isCanDele = YES;
        }
//        if (i == index) {
//            dayView.isAdd = YES;
//
//        }
//        if (i > index ) {
//            dayView.isAdd = YES;
//            dayView.hidden = YES;
//        }
    }
}

- (void)addTime:(NSArray *)times by:(NSInteger)index {
    
    BRMDayTimeView *dayView = (BRMDayTimeView *)[_contentView viewWithTag:100 + index];
    if (times.count == 2) {
        
        NSString *str = [times componentsJoinedByString:@" - "];
        
        dayView.timeStr = str;
    }
}

#pragma mark - UI --
- (void)setUI {
    
    UIView *head = [[UIView alloc] init];
    [self addSubview:head];
    UIView *contentView = [[UIView alloc] init];
    [self addSubview:contentView];
    _headerView = head;
    _contentView = contentView;
    [head mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.mas_equalTo(44);
    }];
    
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.offset(0);
    }];
    
    [head mas_updateConstraints:^(MASConstraintMaker *make) {
        _headViewBottomContact0 = make.bottom.offset(0);
        _headViewBottomContact1 = make.bottom.equalTo(_contentView.mas_top).offset(0);
    }];
    
    self.isOn = NO;
    
    BRMSwitchUIView *switchBtn = [[BRMSwitchUIView alloc] init];
    [head addSubview:switchBtn];
    [switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(-16);
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(24);
    }];
    
    QMUILabel *dayL = [BRToolsView br_label:@"--" textColor:kColorTitle font:[UIFont fontM:16]];
    [head addSubview:dayL];
    [dayL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.centerY.equalTo(switchBtn.mas_centerY).offset(0);
    }];
    _titleL = dayL;
    
    BRMSwitchUIView *timeSwitchBtn = [[BRMSwitchUIView alloc] init];
    [contentView addSubview:timeSwitchBtn];
    [timeSwitchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10);
        make.right.offset(-16);
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(24);
//        make.bottom.offset(-56);
    }];
    
    QMUILabel *hours = [BRToolsView br_label:Localized(@"shop_24Hours") textColor:kColorTitle font:[UIFont fontM:16]];
    [contentView addSubview:hours];
    [hours mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.centerY.equalTo(timeSwitchBtn.mas_centerY).offset(0);
    }];
    hours.alpha = 0.4;
    _hoursL = hours;
    
    UIView *timeBgView = [[UIView alloc] init];
    timeBgView.layer.masksToBounds = YES;
    [contentView addSubview:timeBgView];
    [timeBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.mas_equalTo(46);
        make.top.offset(44);
    }];
    _timeBGView = timeBgView;
    
    CGFloat day_w = (kScreenWidth - 74.) / 2.0;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            NSInteger index = i * 2 + j;
            BRMDayTimeView *dayView = [[BRMDayTimeView alloc] init];
            dayView.tag = 100 + index;
            [timeBgView addSubview:dayView];
            [dayView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(day_w);
                make.height.mas_equalTo(36);
                make.top.offset(46 * i);
                make.left.offset(16 + (day_w + 10) * j);
            }];
            dayView.isEnble = YES;
            if (index == 0) {
                dayView.isCanDele = NO;
                dayView.isAdd = NO;
            }else {
                dayView.isCanDele = NO;
                dayView.isAdd = YES;
            }
            dayView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toSetTime:)];
            [dayView addGestureRecognizer:tap];
            WS(weakSelf);
            dayView.deleteTime = ^(BRMDayTimeView * _Nonnull dayView) {
                [weakSelf deleteDayTime:dayView];
            };
        }
        
     }
    
    UIView *line = [BRToolsView getLineView];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.left.offset(16);
        make.right.offset(0);
        make.bottom.offset(0);
    }];
    _lineView = line;
    _daySwitchBtn = switchBtn;
    _allTimeSwitchBtn = timeSwitchBtn;
    WS(weakSelf);
    switchBtn.switchClickBlock = ^(BOOL isOn) {
        weakSelf.isOn = isOn;
        [weakSelf switchBtn:isOn type:0];
    };
    timeSwitchBtn.switchClickBlock = ^(BOOL isOn) {
        weakSelf.isHoursOn = isOn;
        [weakSelf switchBtn:isOn type:1];
    };
    
}

- (void)switchBtn:(BOOL)isOn type:(NSInteger)type {
        
    if (self.switchBlock) {
        self.switchBlock(isOn, type);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
